package com.floreantpos.bo.ui;

import com.floreantpos.bo.actions.*;
import com.floreantpos.config.ApplicationConfig;
import com.floreantpos.main.Application;
import com.floreantpos.model.User;
import com.floreantpos.model.UserPermission;
import com.floreantpos.model.UserType;
import com.floreantpos.ui.report.actions.*;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.*;
import java.util.Set;
import javax.swing.*;

/**
 *
 * @author MShahriar
 */
public class BackOfficeWindow extends javax.swing.JFrame {

  /**
   * Creates new form BackOfficeWindow
   */
  public BackOfficeWindow() {
    initComponents();

    createMenus();

    tabbedPane.addMouseListener(new MouseAdapter() {
      @Override
      public void mouseClicked(MouseEvent e) {
        if (e.getClickCount() == 2 && !e.isConsumed()) {
          int index = tabbedPane.getSelectedIndex();
          tabbedPane.remove(index);
        }
      }
    });

    positionWindow();

    setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        ApplicationConfig.getPreferences().putInt("bwwidth", BackOfficeWindow.this.getWidth());
        ApplicationConfig.getPreferences().putInt("bwheight", BackOfficeWindow.this.getHeight());
        ApplicationConfig.getPreferences().putInt("bwx", BackOfficeWindow.this.getX());
        ApplicationConfig.getPreferences().putInt("bwy", BackOfficeWindow.this.getY());
        Application.getInstance().setBackOfficeWindow(null);
        dispose();
      }
    });

    setTitle(Application.getTitle() + " - " + com.floreantpos.POSConstants.BACK_OFFICE);
  }

  private void positionWindow() {
    int width = ApplicationConfig.getPreferences().getInt("bwwidth", 800);
    int height = ApplicationConfig.getPreferences().getInt("bwheight", 600);
    setSize(width, height);

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (screenSize.width - width) >> 1;
    int y = (screenSize.height - height) >> 1;

    x = ApplicationConfig.getPreferences().getInt("bwx", x);
    y = ApplicationConfig.getPreferences().getInt("bwy", y);

    setLocation(x, y);
  }

  private void createMenus() {
    User user = Application.getCurrentUser();
    UserType newUserType = user.getNewUserType();

    Set<UserPermission> permissions = null;

    if (newUserType != null) {
      permissions = newUserType.getPermissions();
    }

    JMenuBar menuBar = new JMenuBar();

    if (newUserType == null) {
      createAdminMenu(menuBar);
      createExplorerMenu(menuBar);
      createReportMenu(menuBar);
      createEcfMenu(menuBar);
    } else {
      if (permissions != null && permissions.contains(UserPermission.PERFORM_ADMINISTRATIVE_TASK)) {
        createAdminMenu(menuBar);
      }
      if (permissions != null && permissions.contains(UserPermission.VIEW_EXPLORERS)) {
        createExplorerMenu(menuBar);
      }
      if (permissions != null && permissions.contains(UserPermission.VIEW_REPORTS)) {
        createReportMenu(menuBar);
      }
      if (!Application.isTestSystem() && permissions != null && permissions.contains(UserPermission.PERFORM_ADMINISTRATIVE_TASK)) {
        createEcfMenu(menuBar);
      }
    }

    setJMenuBar(menuBar);
  }

  private void createReportMenu(JMenuBar menuBar) {
    JMenu reportMenu = new JMenu(com.floreantpos.POSConstants.REPORTS);
    reportMenu.add(new SalesReportAction());
    reportMenu.add(new GuestSalesReportAction());
    reportMenu.add(new OpenTicketSummaryReportAction());
    reportMenu.add(new HourlyLaborReportAction());
    reportMenu.add(new KeyStatisticsSalesReportAction());
    reportMenu.add(new CreditCardReportAction());
    reportMenu.add(new MenuUsageReportAction());
    reportMenu.add(new ServerProductivityReportAction());
    reportMenu.add(new JournalReportAction());
    reportMenu.add(new SalesBalanceReportAction());
    reportMenu.add(new SalesExceptionReportAction());
    menuBar.add(reportMenu);
  }

  private void createExplorerMenu(JMenuBar menuBar) {
    JMenu explorerMenu = new JMenu(com.floreantpos.POSConstants.EXPLORERS);
    explorerMenu.add(new CategoryExplorerAction());
    explorerMenu.add(new GroupExplorerAction());
    explorerMenu.add(new ItemExplorerAction());
    explorerMenu.add(new ModifierGroupExplorerAction());
    explorerMenu.add(new ModifierExplorerAction());
    explorerMenu.add(new CookingInstructionExplorerAction());
    explorerMenu.add(new CouponExplorerAction());
    explorerMenu.add(new GuestExplorerAction());
    menuBar.add(explorerMenu);
  }

  private void createAdminMenu(JMenuBar menuBar) {
    JMenu adminMenu = new JMenu(com.floreantpos.POSConstants.ADMIN);
    adminMenu.add(new ConfigureRestaurantAction());
    adminMenu.add(new UserTypeExplorerAction());
    adminMenu.add(new UserExplorerAction());
    adminMenu.add(new ViewGratuitiesAction());
    menuBar.add(adminMenu);
  }
  
  private void createEcfMenu(JMenuBar menuBar) {
    JMenu ecfMenu = new JMenu("Funções do ECF");
    ecfMenu.add(new RpsSaoPauloAction());
    /*
    if (Application.getECFStatus() == 2) {
      ecfMenu.add(new NotaFiscalPaulistaAction());
    }
    */
    menuBar.add(ecfMenu);
  }  

  private void initComponents() {
    jPanel1 = new javax.swing.JPanel();
    tabbedPane = new javax.swing.JTabbedPane();

    getContentPane().setLayout(new java.awt.BorderLayout(5, 0));

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    jPanel1.setLayout(new java.awt.BorderLayout(5, 0));

    jPanel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
    jPanel1.add(tabbedPane, java.awt.BorderLayout.CENTER);

    getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

    pack();
  }

  /**
   * @param args the command line arguments
   */
  public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
        new BackOfficeWindow().setVisible(true);
      }
    });
  }

  private javax.swing.JPanel jPanel1;
  private javax.swing.JTabbedPane tabbedPane;

  public javax.swing.JTabbedPane getTabbedPane() {
    return tabbedPane;
  }

}
